package com.gddb.xwj.mywiki.service;

import com.gddb.xwj.mywiki.domain.User;
import com.gddb.xwj.mywiki.domain.UserExample;
import com.gddb.xwj.mywiki.exception.BusinessException;
import com.gddb.xwj.mywiki.exception.BusinessExceptionEnum;
import com.gddb.xwj.mywiki.mapper.UserMapper;
import com.gddb.xwj.mywiki.req.UserLoginReq;
import com.gddb.xwj.mywiki.req.UserQueryReq;
import com.gddb.xwj.mywiki.req.UserResetPasswordReq;
import com.gddb.xwj.mywiki.req.UserSaveReq;
import com.gddb.xwj.mywiki.resp.UserLoginResp;
import com.gddb.xwj.mywiki.resp.UserQueryResp;
import com.gddb.xwj.mywiki.resp.PageResp;
import com.gddb.xwj.mywiki.util.CopyUtil;
import com.gddb.xwj.mywiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：电子书
 *
 * @author xwj
 * @create 2022-03-19 17:53
 */
@Service
@Slf4j
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<UserQueryResp> list(UserQueryReq req){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andLoginNameLike("%" + req.getLoginName() + "%");
        }
        //只会对第一个遇到的select生效
        PageHelper.startPage(req.getPage(),req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);
        // 列表复制
        List<UserQueryResp> list = CopyUtil.copyList(userList, UserQueryResp.class);

        PageInfo<User> pageInfo = new PageInfo<>(userList);
        //注意点 PageInfo<UserQueryResp> pageInfo = new PageInfo<>(list); 如果pageinfo用的是UserQueryResp 则返回的是分页的总数 必须为user才是全部
        log.info("总行数 :{}", pageInfo.getTotal());
        log.info("总页数 :{}", pageInfo.getPages());

        PageResp<UserQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
     * 保存
     * @param req
     */
    public void save(UserSaveReq req) {
        User user = CopyUtil.copy(req, User.class);
        if(ObjectUtils.isEmpty(req.getId())){
            User userDB = selectByLoginName(user.getLoginName());
            if(ObjectUtils.isEmpty(userDB)){
                //登录用户不存在则 新增
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else{
                throw new BusinessException(BusinessExceptionEnum.USER_LOGIN_NAME_EXITS);
            }
        }else{
            //修改
//            userMapper.updateByPrimaryKey(user);
            //仅对有值的参数更新
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    /**
     * 查看登录名是否存在
     * @param loginName
     * @return
     */
    public User selectByLoginName(String loginName){
        UserExample userExample = new UserExample();
        userExample.createCriteria().andLoginNameEqualTo(loginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }
        return userList.get(0);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 重置密码
     * @param req
     */
    public void resetPassword(UserResetPasswordReq req) {
        User user = CopyUtil.copy(req, User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    public UserLoginResp login(UserLoginReq req) {
        //先根据登录名查询数据库再比对密码
        User userDb = selectByLoginName(req.getLoginName());
        if (ObjectUtils.isEmpty(userDb)) {
            // 用户名不存在
            log.info("用户名不存在, {}", req.getLoginName());
            throw new BusinessException(BusinessExceptionEnum.LOGIN_USER_ERROR);
        }
        if (!userDb.getPassword().equals(req.getPassword())) {
            // 密码不对
            log.info("密码不对, 输入密码：{}, 数据库密码：{}", req.getPassword(), userDb.getPassword());
            throw new BusinessException(BusinessExceptionEnum.LOGIN_USER_ERROR);
        }
        // 登录成功
        UserLoginResp userLoginResp = CopyUtil.copy(userDb, UserLoginResp.class);
        return userLoginResp;
    }
}
